<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use App\Http\Requests;
use DB;
class CarouselController extends CommonController
{
    
    public function index()
    {	
        $list = DB::table("carousel")->paginate(3);
    	return  view("admin.carousel.index",["list"=>$list]);
       	
    }
    //加载添加表单
    public function create()
    {
         return view("admin.carousel.add");   
    }
    //执行添加动作
    public function store(Request $request)
    {
        if(!empty($request->img)){
           //执行图片上传
            if($request->hasFile('img')) {
                //获取上文件信息
            // var_dump($_FILES);
                $file = $request->file('img');
                //获取上传文件的后缀名
                $ext = $file->getClientOriginalExtension();
                //随机一个新的文件名
                $filename = time().rand(1000,9999).".".$ext;
                //执行文件移动
                $newfile = $file->move("./uploads/",$filename);
                //执行图片缩放
                $zimg = Image::make("./uploads/".$filename)->resize(100,100);
                $zimg->save("./uploads/s_".$filename);  

                $data = ["dzur"=>$request->dzur,"img"=>"s_".$filename];
                 DB::table("carousel")->insert($data);
                $list = DB::table("carousel")->paginate(3);

                return view("admin.carousel.index",["list"=>$list]);
            }else{
                return ("图片添加失败");
            }

        }else{
            return ("请添加完整信息");
        }
    }
    //加载修改表单
    public function edit($id)
    {   

        $list = DB::table("carousel")->where("id",$id)->paginate(3);
        return  view("admin.carousel.update",["list"=>$list]);
    }
    //执行修改动作
    public function update(Request $request,$id)
    {             
        if(!empty($request->img)){
            $si = DB::table("carousel")->where("id",$id)->get();
            foreach ($si as $v) {
                @unlink("uploads/$v->img");
            }

            if($request->Hasfile("img")){
                //获取文件上传信息
                $file = $request->file('img');
                //获取上传文件的后缀名
                $ext = $file->getClientOriginalExtension();
                //随机一个新的文件名
                $filename = time().rand(1000,9999).".".$ext;
                //执行文件移动
                $newfile = $file->move("./uploads/",$filename);

                //执行图片缩放
                $zimg = Image::make("./uploads/".$filename)->resize(100,100);
                $zimg->save("./uploads/s_".$filename);  

                $data = ["dzur"=>$request->dzur,"img"=>"s_".$filename];
                 
                 DB::table("carousel")->where('id',$id)->update($data);
                
                $list = DB::table("carousel")->paginate(3);
                return view("admin.carousel.index",["list"=>$list]);    


            }else{
                return "图片获取失败";
            }
        
        }else{
            $data = ['dzur'=>$request->dzur];
            DB::table("carousel")->where("id",$id)->update($data);

            $list = DB::table("carousel")->paginate(3);
            
            return view("admin.carousel.index",["list"=>$list]); 
        }
    }
    //执行删除动作
    public function destory($id)
    {
        DB::table("carousel")->delete($id);

        $list = DB::table("carousel")->paginate(3);
            
        return redirect("admin.carousel.index",["list"=>$list]); 
    } 
}